package com.feng.gaoerfu.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;

/**
 * @author zeyu.jia
 * @version $Rev$
 * @time 2016/6/13 0013 13:27
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDate $Date$
 * @updateDes ${TODO}
 */
public abstract class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";

    /**
     * 该fragment全局视图，不能在子Fragment中创建
     */
    protected View view = null;

    /**
     * 在onCreateView方法中赋值，不能在子Fragment中创建
     */
    protected BaseActivity mContext = null;

    public boolean isLoaded = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = (BaseActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = initView();
        ButterKnife.inject(this, view);
        initData();
        initListener();

        return view;
    }

    /**
     * UI显示方法，必须在子类onCreateView方法内调用
     */
    public View initView(){
        View view = View.inflate(mContext, initContentView(),null);
        return view;
    }

    protected abstract int initContentView();

    /**
     * data数据方法，必须在子类onCreateView方法内调用
     */
    public abstract void initData();

    /**
     * listener事件监听方法，必须在子类onCreateView方法内调用
     */
    public abstract void initListener();


    //显示与关闭进度弹窗方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    /**展示加载进度条,无标题
     * @param stringResId
     */
    public void showProgressDialog(final int stringResId){
        mContext.showProgressDialog(mContext.getResources().getString(stringResId));
    }
    /**展示加载进度条,无标题
     * @param dialogMessage
     */
    public void showProgressDialog(final String dialogMessage){
        mContext.showProgressDialog(dialogMessage);
    }
    /**展示加载进度条
     * @param dialogTitle 标题
     * @param dialogMessage 信息
     */
    public void showProgressDialog(final String dialogTitle, final String dialogMessage){
        mContext.showProgressDialog(dialogTitle, dialogMessage);
    }

    /** 隐藏加载进度
     */
    public void dismissProgressDialog(){
        mContext.dismissProgressDialog();
    }
    //显示与关闭进度弹窗方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //show short toast 方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    /**快捷显示short toast方法，需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
     * @param stringResId
     */
    public void showShortToast(final int stringResId) {
        mContext.showShortToast(stringResId);
    }
    /**快捷显示short toast方法，需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
     * @param string
     */
    public void showShortToast(final String string) {
        mContext.showShortToast(string);
    }
    /**快捷显示short toast方法，需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
     * @param string
     * @param isForceDismissProgressDialog
     */
    public void showShortToast(final String string, final boolean isForceDismissProgressDialog) {
        mContext.showShortToast(string, isForceDismissProgressDialog);
    }
    //show short toast 方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //启动新Activity方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**打开新的Activity，向左滑入效果
     * @param intent
     */
    public void toActivity(final Intent intent) {
        toActivity(intent, true);
    }
	/*public void toActivity(final Intent intent,final boolean isFinish) {
		toActivity(intent, -1, true);
	}*/
    /**打开新的Activity
     * @param intent
     * @param showAnimation
     */
    public void toActivity(final Intent intent, final boolean showAnimation) {
        toActivity(intent, -1, showAnimation);
    }
    /**打开新的Activity，向左滑入效果
     * @param intent
     * @param requestCode
     */
    public void toActivity(final Intent intent, final int requestCode) {
        toActivity(intent, requestCode, true);
    }
    /**打开新的Activity
     * @param intent
     * @param requestCode
     * @param showAnimation
     */
    public void toActivity(final Intent intent, final int requestCode, final boolean showAnimation) {
        if (intent == null) {
            Log.e(TAG, "toActivity  isAlive == false || intent == null >> return;");
            return;
        }
        mContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (requestCode < 0) {
                    startActivity(intent);
                } else {
                    startActivityForResult(intent, requestCode);
                }
              /*  if (showAnimation) {
                    mContext.overridePendingTransition(R.anim.right_push_in, R.anim.hold);
                } else {
                    mContext. overridePendingTransition(R.anim.null_anim, R.anim.null_anim);
                }*/
            }
        });

    }
    //启动新Activity方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.reset(this);
    }
}
